/**
 * created by 小卷毛, 2021-08-03
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import androidx.lifecycle.MutableLiveData
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.model.*
import com.xinnuo.common.extend.optNotEmpty
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import org.json.JSONObject

/**
 * 描 述：我的钱包操作相关
 * 作 者：小卷毛  2021-08-03 10:34
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class WalletViewModel : CommonViewModel() {

    val mBalanceEvent = MutableLiveData("{}")
    val mCoinEvent = MutableLiveData("{}")

    /**
     * 钱包信息
     */
    fun wallet() =
        request(
            isLoadingBefore = false,
            isLoadingAfter = false,
            requestUrl = BaseUrl.wallet,
            requestMethod = HttpMethod.GET,
            onSuccess = { mBalanceEvent.value = it }
        )

    /**
     * 会员卡信息
     */
    fun walletCard() =
        request(
            isLoadingBefore = false,
            isLoadingAfter = false,
            requestUrl = BaseUrl.walletMer,
            requestMethod = HttpMethod.GET,
            onSuccess = {
                mCoinEvent.value = JSONObject(it).optNotEmpty("card", "{}")
            }
        )

    /**
     * 账单流水
     */
    fun billList(
        page: Int,
        billDate: String,
        onSuccess: (ArrayList<WalletBillData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<WalletBillData>>>(BaseUrl.walletBil)
            .params(
                getParams(
                    "billDate" to billDate,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<WalletBillData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 提现记录
     */
    fun drawList(
        page: Int,
        onSuccess: (ArrayList<DrawRecord>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<DrawRecord>>>(BaseUrl.walletRcd)
            .params(
                getParams(
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<DrawRecord>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 金币流水
     */
    fun coinList(
        page: Int,
        cardId: String,
        onSuccess: (ArrayList<WalletBillData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<WalletBillData>>>(BaseUrl.walletCon)
            .params(
                getParams(
                    "id" to cardId,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<WalletBillData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 会员账单流水
     */
    fun memberBillList(
        page: Int,
        cardId: String,
        onSuccess: (ArrayList<WalletBillData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<WalletBillData>>>(BaseUrl.memberRecd)
            .params(
                getParams(
                    "id" to cardId,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<WalletBillData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

}